/*
 * @Author: 张笑天
 * @Date: 2018-03-07 17:13:15
 * @Last Modified by: lyuan2018
 * @Last Modified time: 2020-09-10 17:55:07
 */
import React from 'react';
import Route from 'react-router/lib/Route';
import { WeaLocaleProvider } from 'ecCom';

import EmailNewForDialog from './components/EmailNewForDialog.js';
import EmailSetting from './components/emailSetting';
import Account from './components/emailSetting/account';
import Conventional from './components/emailSetting/conventional';
import Template from './components/emailSetting/template';
import Sign from './components/emailSetting/sign';
import Rule from './components/emailSetting/rule';
import Lable from './components/emailSetting/label';
import Folder from './components/emailSetting/folder';
import AutoRespond from './components/emailSetting/autoRespond';
import Blacklist from './components/emailSetting/blacklist';
import AutoClearRule from './components/emailSetting/autoClearRule';

import EmailUpgrade from './components/home';
import StaticView from './components/staticView';
import FilePreview from './components/filePreview';
import './style';
import stores from './stores';
import { nameLowerCase, listRoute } from './util/index';

const getLocaleLabel = WeaLocaleProvider.getLocaleLabel.bind(this, 'email');

const req = require.context('./', true, /\.\/components\/\w+\.js/);
let list = {};
let routes = req.keys().map((mod) => {
  const path = nameLowerCase(mod.replace(/\.\/components\/|\.js/g, () => ''));
  const component = req(mod).default;
  const r = {
    key: path,
    path,
    component,
  };
  if (path === 'list') list = { ...r };
  return r;
});

listRoute.forEach(path =>
  routes.push({
    ...list,
    key: path,
    path,
  }),
);

const filterKeys = ['main', 'list', 'setting'];

routes = routes.filter(r => filterKeys.indexOf(r.path) < 0);

const Routes = [
  <Route
    ecId={`${(this && this.props && this.props.ecId) || ''}_Route@et3oc2@email`}
    key="email"
    path="email"
    onEnter={getLocaleLabel}
    component={EmailUpgrade}
  >
    {routes.map(r => (
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@4agv82@${r.key}`}
        {...r}
      />
    ))}
    <Route
      ecId={`${
        (this && this.props && this.props.ecId) || ''
      }_Route@hmcssb@setting`}
      key="setting"
      path="setting"
      component={EmailSetting}
    >
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@zogtt6@mailAccount`}
        key="mailAccount"
        path="mailAccount"
        component={Account}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@rf79kk@mailSetting`}
        key="mailSetting"
        path="mailSetting"
        component={Conventional}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@2djevr@mailTemplate`}
        key="mailTemplate"
        path="mailTemplate"
        component={Template}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@8pp0bq@mailSign`}
        key="mailSign"
        path="mailSign"
        component={Sign}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@ngcheq@mailRule`}
        key="mailRule"
        path="mailRule"
        component={Rule}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@qj7yt3@labelManage`}
        key="labelManage"
        path="labelManage"
        component={Lable}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@0da3sv@folderManage`}
        key="folderManage"
        path="folderManage"
        component={Folder}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@sfjq1t@mailAutoRespond`}
        key="mailAutoRespond"
        path="mailAutoRespond"
        component={AutoRespond}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@as3067@mailblacklist`}
        key="mailblacklist"
        path="mailblacklist"
        component={Blacklist}
      />
      <Route
        ecId={`${
          (this && this.props && this.props.ecId) || ''
        }_Route@02voia@autoClearRule`}
        key="autoClearRule"
        path="autoClearRule"
        component={AutoClearRule}
      />
    </Route>
  </Route>,
  <Route
    ecId={`${
      (this && this.props && this.props.ecId) || ''
    }_Route@za15ff@staticView`}
    name="staticView"
    breadcrumbName="邮件静态查看页面"
    path="staticView"
    component={StaticView}
  />,
  <Route
    ecId={`${
      (this && this.props && this.props.ecId) || ''
    }_Route@bxtk4f@preview`}
    name="preview"
    breadcrumbName="附件预览页面"
    path="preview"
    component={FilePreview}
  />,
];

module.exports = {
  Route: Routes,
  store: stores,
  components: {
    EmailNewForDialog,
  },
};
